/*
 * File: DataTypeDeserializer.java
 *
 * a deserialising helper class for the EfficientSoap data TYPEs,
 * using the EfficientSoap.org SOAP client library project.
 *
 * @author Christian Rau
 */

package org.efficientsoap.test.deserializer;

import android.util.Log;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.efficientsoap.client.EfficientSoapFaultException;
import org.efficientsoap.client.*;
import org.efficientsoap.test.datatypes.*;



/**
 * a deserialising helper class for the EfficientSoap data TYPEs ,
 * using the EfficientSoap.org SOAP client library project.
 *
 *
 * @author Christian Rau
 */
public class DataTypeDeserializer{

	private static final String TAG = DataTypeDeserializer.class.getSimpleName();

	private static final String NAMESPACE = "http://efficietsoap.org/test/datatypes";

	public DataTypeDeserializer() {
		super();
	}


	public static ComplexData parseComplexData(EfficientSoapDeserializer<?> eSoapDeserializer, String tag) throws XmlPullParserException, IOException, EfficientSoapFaultException
	{
		Log.d(TAG, "parseComplexData()");
		ComplexData complexData = new ComplexData();
		Boolean isprimitiveDataUnReaded = true;
		Boolean issimpleDataUnReaded = true;
		Boolean issimpleListUnReaded = true;
		if(null != tag){
			eSoapDeserializer.getParser().require(XmlPullParser.START_TAG, NAMESPACE, tag);
			eSoapDeserializer.getParser().nextToken();
			eSoapDeserializer.skipWhitespace();
		}
		while (eSoapDeserializer.getParser().getEventType() == XmlPullParser.START_TAG) {
		if (eSoapDeserializer.getParser().getName().equals("primitiveData") && isprimitiveDataUnReaded){
 				complexData.setPrimitiveData(parsePrimitiveData(eSoapDeserializer, "primitiveData"));
				isprimitiveDataUnReaded = false;
				eSoapDeserializer.skipWhitespace();
			}
		else		if (eSoapDeserializer.getParser().getName().equals("simpleData") && issimpleDataUnReaded){
 				complexData.setSimpleData(parseSimpleData(eSoapDeserializer, "simpleData"));
				issimpleDataUnReaded = false;
				eSoapDeserializer.skipWhitespace();
			}
		else		if (eSoapDeserializer.getParser().getName().equals("simpleList") && issimpleListUnReaded){
 				complexData.setSimpleList((ArrayList<SimpleData>) parseSimpleDataList(eSoapDeserializer, "simpleList"));
				issimpleListUnReaded = false;
				eSoapDeserializer.skipWhitespace();
			}
			else
				throw new XmlPullParserException("The Tag <"+eSoapDeserializer.getParser().getName()
							+"> was not expected "
							+"The Expecting Tag must be one Kind of this: "+
							"<primitiveData>, "
+							"<simpleData>, "
+							"<simpleList>, "
							+"or is already processed"
				);
		eSoapDeserializer.skipWhitespace();
		}
		if(null != tag){
			eSoapDeserializer.skipWhitespace();
			eSoapDeserializer.getParser().require(XmlPullParser.END_TAG, NAMESPACE, tag);
			eSoapDeserializer.getParser().nextToken();
		}
		return complexData;
	}

	public static PrimitiveData parsePrimitiveData(EfficientSoapDeserializer<?> eSoapDeserializer, String tag) throws XmlPullParserException, IOException, EfficientSoapFaultException
	{
		Log.d(TAG, "parsePrimitiveData()");
		PrimitiveData primitiveData = new PrimitiveData();
		Boolean isintegerValueUnReaded = true;
		Boolean islongValueUnReaded = true;
		Boolean isdoubleValueUnReaded = true;
		Boolean isbooleanValueUnReaded = true;
		if(null != tag){
			eSoapDeserializer.getParser().require(XmlPullParser.START_TAG, NAMESPACE, tag);
			eSoapDeserializer.getParser().nextToken();
			eSoapDeserializer.skipWhitespace();
		}
		while (eSoapDeserializer.getParser().getEventType() == XmlPullParser.START_TAG) {
		if (eSoapDeserializer.getParser().getName().equals("integerValue") && isintegerValueUnReaded){
 				primitiveData.setIntegerValue(eSoapDeserializer.parseIntegerElem("integerValue"));
				isintegerValueUnReaded = false;
				eSoapDeserializer.skipWhitespace();
			}
		else		if (eSoapDeserializer.getParser().getName().equals("longValue") && islongValueUnReaded){
 				primitiveData.setLongValue(eSoapDeserializer.parseAmountElem("longValue"));
				islongValueUnReaded = false;
				eSoapDeserializer.skipWhitespace();
			}
		else		if (eSoapDeserializer.getParser().getName().equals("doubleValue") && isdoubleValueUnReaded){
 				primitiveData.setDoubleValue(eSoapDeserializer.parseDoubleElem("doubleValue"));
				isdoubleValueUnReaded = false;
				eSoapDeserializer.skipWhitespace();
			}
		else		if (eSoapDeserializer.getParser().getName().equals("booleanValue") && isbooleanValueUnReaded){
 				primitiveData.setBooleanValue(eSoapDeserializer.parseBooleanElem("booleanValue"));
				isbooleanValueUnReaded = false;
				eSoapDeserializer.skipWhitespace();
			}
			else
				throw new XmlPullParserException("The Tag <"+eSoapDeserializer.getParser().getName()
							+"> was not expected "
							+"The Expecting Tag must be one Kind of this: "+
							"<integerValue>, "
+							"<longValue>, "
+							"<doubleValue>, "
+							"<booleanValue>, "
							+"or is already processed"
				);
		eSoapDeserializer.skipWhitespace();
		}
		if(null != tag){
			eSoapDeserializer.skipWhitespace();
			eSoapDeserializer.getParser().require(XmlPullParser.END_TAG, NAMESPACE, tag);
			eSoapDeserializer.getParser().nextToken();
		}
		return primitiveData;
	}

	public static RecursiveData parseRecursiveData(EfficientSoapDeserializer<?> eSoapDeserializer, String tag) throws XmlPullParserException, IOException, EfficientSoapFaultException
	{
		Log.d(TAG, "parseRecursiveData()");
		RecursiveData recursiveData = new RecursiveData();
		Boolean iscomplexDataUnReaded = true;
		Boolean iscomplexListUnReaded = true;
		Boolean isrecursiveDataUnReaded = true;
		if(null != tag){
			eSoapDeserializer.getParser().require(XmlPullParser.START_TAG, NAMESPACE, tag);
			eSoapDeserializer.getParser().nextToken();
			eSoapDeserializer.skipWhitespace();
		}
		while (eSoapDeserializer.getParser().getEventType() == XmlPullParser.START_TAG) {
		if (eSoapDeserializer.getParser().getName().equals("complexData") && iscomplexDataUnReaded){
 				recursiveData.setComplexData(parseComplexData(eSoapDeserializer, "complexData"));
				iscomplexDataUnReaded = false;
				eSoapDeserializer.skipWhitespace();
			}
		else		if (eSoapDeserializer.getParser().getName().equals("complexList") && iscomplexListUnReaded){
 				recursiveData.setComplexList((ArrayList<ComplexData>) parseComplexDataList(eSoapDeserializer, "complexList"));
				iscomplexListUnReaded = false;
				eSoapDeserializer.skipWhitespace();
			}
		else		if (eSoapDeserializer.getParser().getName().equals("recursiveData") && isrecursiveDataUnReaded){
 				recursiveData.setRecursiveData(parseRecursiveData(eSoapDeserializer, "recursiveData"));
				isrecursiveDataUnReaded = false;
				eSoapDeserializer.skipWhitespace();
			}
			else
				throw new XmlPullParserException("The Tag <"+eSoapDeserializer.getParser().getName()
							+"> was not expected "
							+"The Expecting Tag must be one Kind of this: "+
							"<complexData>, "
+							"<complexList>, "
+							"<recursiveData>, "
							+"or is already processed"
				);
		eSoapDeserializer.skipWhitespace();
		}
		if(null != tag){
			eSoapDeserializer.skipWhitespace();
			eSoapDeserializer.getParser().require(XmlPullParser.END_TAG, NAMESPACE, tag);
			eSoapDeserializer.getParser().nextToken();
		}
		return recursiveData;
	}

	public static SimpleData parseSimpleData(EfficientSoapDeserializer<?> eSoapDeserializer, String tag) throws XmlPullParserException, IOException, EfficientSoapFaultException
	{
		Log.d(TAG, "parseSimpleData()");
		SimpleData simpleData = new SimpleData();
		Boolean isstringValueUnReaded = true;
		Boolean isintegerListUnReaded = true;
		Boolean isprimitiveDataUnReaded = true;
		if(null != tag){
			eSoapDeserializer.getParser().require(XmlPullParser.START_TAG, NAMESPACE, tag);
			eSoapDeserializer.getParser().nextToken();
			eSoapDeserializer.skipWhitespace();
		}
		while (eSoapDeserializer.getParser().getEventType() == XmlPullParser.START_TAG) {
		if (eSoapDeserializer.getParser().getName().equals("stringValue") && isstringValueUnReaded){
 				simpleData.setStringValue(eSoapDeserializer.parseStringElem("stringValue"));
				isstringValueUnReaded = false;
				eSoapDeserializer.skipWhitespace();
			}
		else		if (eSoapDeserializer.getParser().getName().equals("integerList") && isintegerListUnReaded){
 				simpleData.setIntegerList((ArrayList<Integer>) eSoapDeserializer.parseIntegerList("integerList"));
				isintegerListUnReaded = false;
				eSoapDeserializer.skipWhitespace();
			}
		else		if (eSoapDeserializer.getParser().getName().equals("primitiveData") && isprimitiveDataUnReaded){
 				simpleData.setPrimitiveData(parsePrimitiveData(eSoapDeserializer, "primitiveData"));
				isprimitiveDataUnReaded = false;
				eSoapDeserializer.skipWhitespace();
			}
			else
				throw new XmlPullParserException("The Tag <"+eSoapDeserializer.getParser().getName()
							+"> was not expected "
							+"The Expecting Tag must be one Kind of this: "+
							"<stringValue>, "
+							"<integerList>, "
+							"<primitiveData>, "
							+"or is already processed"
				);
		eSoapDeserializer.skipWhitespace();
		}
		if(null != tag){
			eSoapDeserializer.skipWhitespace();
			eSoapDeserializer.getParser().require(XmlPullParser.END_TAG, NAMESPACE, tag);
			eSoapDeserializer.getParser().nextToken();
		}
		return simpleData;
	}


	public static List<Long> parseAmountList(EfficientSoapDeserializer<?> eSoapDeserializer, String tag) throws XmlPullParserException, IOException, EfficientSoapFaultException{
		Log.d(TAG, "parseAmountList("+tag+")");
		List<Long> list = new ArrayList<Long>();
		while (eSoapDeserializer.getParser().getEventType() == XmlPullParser.START_TAG
				&& eSoapDeserializer.getParser().getName().equals(tag)) {
			list.add(eSoapDeserializer.parseAmountElem(tag));
		}
		return list;
	}

	public static List<Boolean> parseBooleanList(EfficientSoapDeserializer<?> eSoapDeserializer, String tag) throws XmlPullParserException, IOException, EfficientSoapFaultException{
		Log.d(TAG, "parseBooleanList("+tag+")");
		List<Boolean> list = new ArrayList<Boolean>();
		while (eSoapDeserializer.getParser().getEventType() == XmlPullParser.START_TAG
				&& eSoapDeserializer.getParser().getName().equals(tag)) {
			list.add(eSoapDeserializer.parseBooleanElem(tag));
		}
		return list;
	}


	public static List<ComplexData> parseComplexDataList(EfficientSoapDeserializer<?> eSoapDeserializer, String tag) throws XmlPullParserException, IOException, EfficientSoapFaultException{
		Log.d(TAG, "parseComplexDataList("+tag+")");
		List<ComplexData> list = new ArrayList<ComplexData>();
		while (eSoapDeserializer.getParser().getEventType() == XmlPullParser.START_TAG
				&& eSoapDeserializer.getParser().getName().equals(tag)) {
			list.add(parseComplexData(eSoapDeserializer, tag));
		}
		return list;
	}

	public static List<Integer> parseIntegerList(EfficientSoapDeserializer<?> eSoapDeserializer, String tag) throws XmlPullParserException, IOException, EfficientSoapFaultException{
		Log.d(TAG, "parseIntegerList("+tag+")");
		List<Integer> list = new ArrayList<Integer>();
		while (eSoapDeserializer.getParser().getEventType() == XmlPullParser.START_TAG
				&& eSoapDeserializer.getParser().getName().equals(tag)) {
			list.add(eSoapDeserializer.parseIntegerElem(tag));
		}
		return list;
	}


	public static List<PrimitiveData> parsePrimitiveDataList(EfficientSoapDeserializer<?> eSoapDeserializer, String tag) throws XmlPullParserException, IOException, EfficientSoapFaultException{
		Log.d(TAG, "parsePrimitiveDataList("+tag+")");
		List<PrimitiveData> list = new ArrayList<PrimitiveData>();
		while (eSoapDeserializer.getParser().getEventType() == XmlPullParser.START_TAG
				&& eSoapDeserializer.getParser().getName().equals(tag)) {
			list.add(parsePrimitiveData(eSoapDeserializer, tag));
		}
		return list;
	}


	public static List<SimpleData> parseSimpleDataList(EfficientSoapDeserializer<?> eSoapDeserializer, String tag) throws XmlPullParserException, IOException, EfficientSoapFaultException{
		Log.d(TAG, "parseSimpleDataList("+tag+")");
		List<SimpleData> list = new ArrayList<SimpleData>();
		while (eSoapDeserializer.getParser().getEventType() == XmlPullParser.START_TAG
				&& eSoapDeserializer.getParser().getName().equals(tag)) {
			list.add(parseSimpleData(eSoapDeserializer, tag));
		}
		return list;
	}
}

